﻿Imports System.Xml.Linq
Imports System.Xml.Schema
Imports System.Xml.Schema.Extensions
Imports <xmlns:log="urn:myLogConfig">
Imports System.Data
Imports System.Diagnostics
Imports System.Windows.Controls
Imports System.Windows.Threading
Imports System.Text.RegularExpressions
Imports System.IO


Public Class MainWindow
    Public Shared moiMeme As MainWindow
    Friend masterDoc As XDocument
    Friend xmlFileName = AppDomain.CurrentDomain.BaseDirectory & "config/configuration.xml"
    Friend xsdFileName = AppDomain.CurrentDomain.BaseDirectory & "config/schema.xsd"

    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
    End Function


    Private Sub MainWindow_Initialized(sender As Object, e As System.EventArgs) Handles Me.Initialized
        moiMeme = Me

        Dim hWnd As IntPtr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle
        If hWnd <> IntPtr.Zero Then
            'Hide the window
            ' 0 = SW_HIDE
            ShowWindow(hWnd, 0)
        End If

    End Sub

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        readXML()

        If Application.isAuto = True Then
            Console.WriteLine("--           Lancement en mode AUTO              --")
            Me.Visibility = Windows.Visibility.Hidden

            Dim p As New ExecuteMoverUC
            p.r = Me
            p.launchScan()
            Me.Close()
        End If

    End Sub

    Friend Sub readXML()

        Try
            'initialisations 
            TreeView1.Items.Clear()

            'chargement du document
            masterDoc = XDocument.Load(xmlFileName)

            'validation contre le schéma XSD
            Dim xst = New XmlSchemaSet()
            xst.Add(Nothing, xsdFileName)
            masterDoc.Validate(xst, AddressOf XSDErrors)

            Dim lesPlateformes = (From plt In masterDoc...<log:plateforme> Select plt).ToList

            For Each unePlateformeXml As XElement In lesPlateformes

                Dim noeudPlateforme As New TreeViewItemWithIcons
                noeudPlateforme.Icon = My.Resources.plateforme.ToImageSource

                'Ajout des plateformes a la racine du treeview
                Dim unePlateforme As New Plateforme(unePlateformeXml)

                'Ajout des serveurs a la racine du plateformes
                Dim serveursXML = unePlateformeXml.<log:serveur>

                For Each unServeurXml As XElement In serveursXML
                    Dim unServeur As New Serveur(unServeurXml, unePlateforme)

                    Dim noeudServeur As New TreeViewItemWithIcons
                    noeudServeur.Icon = My.Resources.server.ToImageSource

                    Dim cheminsXML = unServeurXml.<log:chemin>

                    For Each cheminXml As XElement In cheminsXML
                        Dim unChemin As New Chemin(cheminXml, unServeur)
                        Dim noeudChemin As New TreeViewItemWithIcons
                        noeudChemin.Icon = My.Resources.folder.ToImageSource

                        noeudChemin.HeaderText = unChemin.destDossier
                        noeudChemin.Tag = unChemin
                        noeudChemin.tagXML = cheminXml
                        noeudServeur.Items.Add(noeudChemin)
                    Next

                    noeudServeur.HeaderText = unServeur.nom
                    noeudServeur.Tag = unServeur
                    noeudServeur.tagXML = unServeurXml
                    noeudPlateforme.Items.Add(noeudServeur)

                Next
                noeudPlateforme.Tag = unePlateforme
                noeudPlateforme.tagXML = unePlateformeXml
                noeudPlateforme.HeaderText = unePlateforme.nom
                TreeView1.Items.Add(noeudPlateforme)
            Next

        Catch ex As Exception
            consoleOut.addItim(New ConsoleTypes.erreur("La validation du fichier XML a échouée ", ex.Message))
            Me.IsEnabled = False
        End Try
    End Sub

    Public Sub DoEvents()
        Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, New Action(Sub()
                                                                                      End Sub))
    End Sub

    Private Sub TreeView1Item_selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        Dim elem As TreeViewItemWithIcons = DirectCast(e.Source, TreeViewItemWithIcons)

        Select Case elem.Tag.GetType.Name

            Case "Plateforme"
                Dim maPlateforme = DirectCast(elem.Tag, Plateforme)
                Dim p As New plateformeUC
                p.setInfo(maPlateforme, elem)

                panelDynamic.Children.Clear()
                panelDynamic.Children.Add(p)

            Case "Serveur"
                Dim monServeur = DirectCast(elem.Tag, Serveur)
                Dim p As New serveurUC
                p.setInfo(monServeur, elem)

                panelDynamic.Children.Clear()
                panelDynamic.Children.Add(p)

            Case "Chemin"
                Dim monChemin = DirectCast(elem.Tag, Chemin)
                Dim p As New cheminUC
                p.setInfo(monChemin, elem)

                panelDynamic.Children.Clear()
                panelDynamic.Children.Add(p)

        End Select

    End Sub

    Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
        consoleOut.addItim(New ConsoleTypes.erreur("La validation du fichier XML a échouée ", e.Message))
        Me.IsEnabled = False
    End Sub

    Private Sub tbx_loseFocusOnkeyPress(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim elem As TextBox = DirectCast(e.Source, TextBox)
        elem.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next))
    End Sub

    Private Sub tbx_selectAllOnDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim elem As TextBox = DirectCast(e.Source, TextBox)
        elem.SelectAll()
    End Sub

    Private Sub ITM_chagerfunc(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ITM_chager.Click
        readXML()
    End Sub

    Private Sub ITM_testerChemins_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ITM_testerChemins.Click
        Dim p As New testAllPathsUC

        panelDynamic.Children.Clear()
        panelDynamic.Children.Add(p)
    End Sub

    Private Sub ITM_LogsTomove_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ITM_LogsTomove.Click
        Dim p As New ViewLogsToMoveUC

        panelDynamic.Children.Clear()
        panelDynamic.Children.Add(p)
    End Sub

    Private Sub ITM_moveLogs_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ITM_moveLogs.Click
        Dim p As New ExecuteMoverUC

        panelDynamic.Children.Clear()
        panelDynamic.Children.Add(p)
    End Sub
End Class

